import * as service from "@/service";
import { IList, ICates } from "./interface";
import _ from "lodash";
const list: IList = {
  namespaced: true,
  state: {
    cates: [],
    lists: [],
  },
  actions: {
    async getCates({ commit }) {
      //type  GET_CATES
      const result = await service.fetchCateList();
      commit({
        type: "GET_CATES",
        payload: result?.data?.data,
      });
    },
    async getLists({ commit }, { params }) {
      //type GET_LISTS
      const result = await service.fetchList(params);
      // console.log('result', result);
      commit({
        type: "GET_LISTS",
        payload: result?.data?.content,
      });
    },
  },
  mutations: {
    GET_CATES(state, { payload }) {
      state.cates = payload.map((item: ICates) => {
        item.text = item.name;
        // delete item.name
        return _.omit(item, ["name"]);
      });
    },
    GET_LISTS(state, { payload }) {
      state.lists = payload;
    },
  },
};

export default list;
